package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Coating;
import model.DateUtil;
import model.DryTime;
import model.Subtreatment;
import model.Subware;
import model.SystemDate;
import service.Service;

public class CheckOut extends JFrame {

	private JCheckBox chckbxOld;
	private JButton btnAccept, btnEnd;

	private JList lstStoredSubwares;
	private JTextArea txtaShow;
	private JScrollPane scpProductType;

	private Service service = Service.getInstance();

	private final Controller controller = new Controller();
	private ListController lstController = new ListController();

	public CheckOut() {

		getContentPane().setLayout(null);
		this.setSize(575, 425);

		JPanel panel = new JPanel();
		panel.setLocation(0, 6);
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		panel.setSize(800, 380);

		chckbxOld = new JCheckBox("Smid overskredet mellemvare ud");
		chckbxOld.setBounds(20, 289, 245, 23);
		panel.add(chckbxOld);
		chckbxOld.addActionListener(controller);

		btnAccept = new JButton("Accepter");
		btnAccept.setBounds(20, 335, 119, 23);
		panel.add(btnAccept);
		btnAccept.addActionListener(controller);

		btnEnd = new JButton("Afslut");
		btnEnd.setBounds(162, 335, 119, 23);
		panel.add(btnEnd);
		btnEnd.addActionListener(controller);

		lstStoredSubwares = new JList();
		lstStoredSubwares.setBounds(20, 6, 169, 271);
		panel.add(lstStoredSubwares);
		lstStoredSubwares.addListSelectionListener(lstController);

		txtaShow = new JTextArea();
		txtaShow.setBounds(211, 6, 332, 271);
		panel.add(txtaShow);
		txtaShow.setEditable(false);

		updateList();
	}

	private void updateList() {

		lstStoredSubwares.setListData(service.getStoredSubwares().toArray());
	}

	private class ListController implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent le) {
			if (!lstStoredSubwares.isSelectionEmpty()) {

				Subware subware = (Subware) lstStoredSubwares
						.getSelectedValue();
				Subtreatment st = subware.getStartStops().get(0)
						.getSubtreatment();
				if (st instanceof DryTime) {
					int i = 0;
					Date d1 = DateUtil.createDate(st.getStartStop().get(0)
							.getStart(), ((DryTime) st).getMaxDryTime());
					i = DateUtil.daysDiff(d1, SystemDate.getInstance()
							.getDate());
					txtaShow.setText("Mellemvare: " + subware + "\n"
							+ "Varenr.: " + subware.getWareNr() + "\n"
							+ "Lokation: " + subware.getLocation() + "\n"
							+ "Slut dato: " + d1);
				} else if (st instanceof Coating) {
					System.out.println("Coating");
				}

			}

		}
	}

	private class Controller implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == btnAccept) {
				if (chckbxOld.isSelected()) {
					Subware subware = (Subware) lstStoredSubwares
							.getSelectedValue();
					service.deleteSubware(subware);
					lstStoredSubwares.setListData(service.getStoredSubwares()
							.toArray());
					txtaShow.setText("");
				} else {

				}

			} else if (e.getSource() == btnEnd) {
				setVisible(false);
			}

		}
	}
}
